package com.zangchat.service.impl;


import com.common.core.domain.AjaxResult;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.UpdateResult;
import com.zangchat.domain.ChatUsers;
import com.zangchat.domain.mongoBean.MTrendsBean;
import com.zangchat.service.IMongoDbService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class MongoDbServiceImpl implements IMongoDbService {

    private static final Logger logger = LoggerFactory.getLogger(MongoDbServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存对象
     * @param map
     * @return
     */
    public AjaxResult save(Map<String,Object> map) {
        mongoTemplate.save(map);
        return AjaxResult.success("保存成功");
    }

    /**
     * 查询所有
     * @return
     */
    public List<Book> findAll() {
        return mongoTemplate.findAll(Book.class);
    }

    /***
     * 根据id查询
     * @param id
     * @return
     */
    public ChatUsers getBookById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query, ChatUsers.class);
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return
     */
    public Book getBookByName(String name) {
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query, Book.class);
    }

    /**
     * 更新对象
     *
     * @param book
     * @return
     */
    public String updateBook(ChatUsers book) {
        Query query = new Query(Criteria.where("_id").is(book.getId()));
        Update update = new Update().set("publish", book.getHeadImg()).set("info", book.getUserName()).set("updateTime",
                new Date());
        // updateFirst 更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query, update, Book.class);
        // updateMulti 更新查询返回结果集的全部
        // mongoTemplate.updateMulti(query,update,Book.class);
        // upsert 更新对象不存在则去添加
        // mongoTemplate.upsert(query,update,Book.class);
        return "success";
    }

    /***
     * 删除对象
     * @param users
     * @return
     */
    public String deleteBook(ChatUsers users) {
        mongoTemplate.remove(users);
        return "success";
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public String deleteBookById(String id) {
        // findOne
        ChatUsers book = getBookById(id);
        // delete
        deleteBook(book);
        return "success";
    }

    /**
     * 模糊查询
     * @param search
     * @return
     */
    public List<Book> findByLikes(String search){
        Query query = new Query();
        Criteria criteria = new Criteria();
        //criteria.where("name").regex(search);
        Pattern pattern = Pattern.compile("^.*" + search + ".*$" , Pattern.CASE_INSENSITIVE);
        criteria.where("name").regex(pattern);
        List<Book> lists = mongoTemplate.findAllAndRemove(query, Book.class);
        return lists;
    }

//    @Override
//    public AjaxResult saveTrends(Map<String, Object> map) {
//        return AjaxResult.success("保存成功");
//    }

    @Override
    public AjaxResult saveTrends(MTrendsBean trendsBean) {
        mongoTemplate.save(trendsBean);
        return AjaxResult.success("保存成功");
    }


    @Override
    public MTrendsBean getTrendsById(String targetId) {
        Query query = new Query(Criteria.where("trendsId").is(Integer.parseInt(targetId)));
        return mongoTemplate.findOne(query, MTrendsBean.class);
    }

    @Override
    public AjaxResult updateTrendById(MTrendsBean trendsBean) {
        Query query = new Query(Criteria.where("trendsId").is(trendsBean.getTrendsId()));
        Update update = new Update().set("comment", trendsBean.getComment());
        // updateFirst 更新查询返回结果集的第一条
//        mongoTemplate.updateFirst(query, update, Map.class);
        // upsert 更新对象不存在则去添加
         mongoTemplate.upsert(query,update,MTrendsBean.class);
        // updateMulti 更新查询返回结果集的全部
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, MTrendsBean.class);
        return AjaxResult.success("成功",updateResult);
    }
}
